import CodoBase from "../CodoBase";
import { Application } from "egg";
import { ZKV6000 } from "./type";
import CodoError from "../CodoError";

/**
 * 万傲瑞达 v6000 开放api接口封装
 */
export default class ZKV6000PublicApiManager extends CodoBase {
	protected urls = {
		getOnePerson: "/api/person/get",
		getOnePart: "/api/department/get",
		/** acc门禁模块 */
		acc: {
			deviceList: "/api/device/accList",
			transactionList: "/api/transaction/list",
			transactionDevice: "/api/transaction/device",
			transactionFirstInAndLastOut: "/api/transaction/firstInAndLastOut",
			transactionPerson: "/api/transaction/person",
		},
	};
	constructor(protected app: Application, private config: ZKV6000.public.ZKV6000PublicApiParams) {
		super(app);
	}

	protected getUrl(path: string) {
		let fullPath = this.config.url + path;
		fullPath += `?access_token=${this.config.appkey}`;
		return fullPath;
	}

	/** 查询一个人员的信息 */
	async getOnePerson(pin: string) {
		const fullPath = this.getUrl(this.urls.getOnePerson + "/" + pin);
		const res = await this.app.curl(fullPath, {
			method: "GET",
			dataType: "json",
			contentType: "json",
			rejectUnauthorized: false,
		});
		const data = res.data as ZKV6000.public.IBioPersonInfo;
		if (data.code === 0) {
			return data.data;
		} else {
			throw new CodoError(`获取一个人员信息出错 %s \r\n %s %s`, pin, data.code, data.message);
		}
	}

	/** 查询一个部门的信息 */
	async getOnePart(code: string) {
		const fullPath = this.getUrl(this.urls.getOnePart + "/" + code);
		const res = await this.app.curl(fullPath, {
			method: "GET",
			dataType: "json",
			contentType: "json",
			rejectUnauthorized: false,
		});
		const data = res.data as ZKV6000.public.IBioDepartment;
		if (data.code === 0) {
			return data.data;
		} else {
			throw new CodoError(`获取一个部门信息出错 %s \r\n %s %s`, code, data.code, data.message);
		}
	}

	/** 查询门禁设备列表 */
	async getAccDeviceList(params: { pageNo: number; pageSize: number }) {
		const fullPath = this.getUrl(this.urls.acc.deviceList);
		const res = await this.app.curl(fullPath, {
			method: "GET",
			dataType: "json",
			contentType: "json",
			data: {
				...params,
			},
			rejectUnauthorized: false,
		});
		const data = res.data as { code: number; message: string; data: ZKV6000.public.IAccDeviceObject[] };
		if (data.code === 0) {
			return data.data;
		} else {
			throw new CodoError(`v6000 get acc device list error   \r\n %s %s`, data.code, data.message);
		}
	}

	/** 分页查询acc门禁记录列表 */
	async getAccTransactionList(params: { pageNo: number; pageSize: number; personPin?: string; startDate?: string; endDate?: string }) {
		const fullPath = this.getUrl(this.urls.acc.transactionList);
		const res = await this.app.curl(fullPath, {
			method: "GET",
			dataType: "json",
			contentType: "json",
			rejectUnauthorized: false,
			data: {
				...params,
			},
		});

		const data = res.data as ZKV6000.public.IAccTransactionListResponse;
		if (data.code === 0) {
			return data.data;
		} else {
			throw new CodoError(`获取acc transaction list出错 \r\n %j \r\n %s \r\n %s`, params, data.code, data.message);
		}
	}

	/** 分页查询acc门禁记录，通过设备sn区分过滤 */
	async getAccTransactionListByDevice(sn: string, params: { pageNo: number; pageSize: number; startDate?: string; endDate?: string }) {
		const fullPath = this.getUrl(this.urls.acc.transactionDevice + "/" + sn);
		const res = await this.app.curl(fullPath, {
			method: "GET",
			dataType: "json",
			contentType: "json",
			rejectUnauthorized: false,
			data: {
				...params,
			},
		});

		const data = res.data as ZKV6000.public.IAccTransactionListResponse;
		if (data.code === 0) {
			return data.data;
		} else {
			throw new CodoError(`获取acc transaction list by device出错 \r\n %j \r\n %s \r\n %s`, params, data.code, data.message);
		}
	}

	/** 分页查询acc门禁记录，通过人员区分 */
	async getAccTransactionPerson(pin: string, params: { pageNo: number; pageSize: number; startDate?: string; endDate?: string }) {
		const fullPath = this.getUrl(this.urls.acc.transactionPerson + "/" + pin);
		const res = await this.app.curl(fullPath, {
			method: "GET",
			dataType: "json",
			contentType: "json",
			rejectUnauthorized: false,
			data: {
				...params,
			},
		});

		const data = res.data as ZKV6000.public.IAccTransactionListResponse;
		if (data.code === 0) {
			return data.data;
		} else {
			throw new CodoError(`获取acc transaction person 出错 \r\n %j \r\n %s \r\n %s`, params, data.code, data.message);
		}
	}

	/** 分页查询acc门禁记录，人员进出，每日第一次进和最后一次出 */
	async getAccTransactionFirstInAndLastOut(params: { pin: string; pageNo: number; pageSize: number; startDate?: string; endDate?: string }) {
		const fullPath = this.getUrl(this.urls.acc.transactionFirstInAndLastOut + "/" + params.pin);
		const res = await this.app.curl(fullPath, {
			method: "GET",
			dataType: "json",
			contentType: "json",
			rejectUnauthorized: false,
			data: {
				...params,
			},
		});

		const data = res.data as ZKV6000.public.IAccTransactionFirstInAndLastOutResponse;
		if (data.code === 0) {
			return data.data;
		} else {
			throw new CodoError(`获取acc transaction first in and last out 出错 \r\n %j \r\n %s \r\n %s`, params, data.code, data.message);
		}
	}
}
